package com.kldy8.kldyw.filmfactory.dteam;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import com.andview.refreshview.XRefreshView;
import com.kldy8.kldyw.BaseActivity;
import com.kldy8.kldyw.BaseAppCompatActivity;
import com.kldy8.kldyw.R;
import com.kldy8.kldyw.filmfactory.company.CompanyActivity;
import com.kldy8.kldyw.filmfactory.dteam.bean.Dteam;
import com.kldy8.kldyw.logre.LoginActivity;
import com.kldy8.kldyw.system.ServerApi;
import com.kldy8.kldyw.system.ServerCallBack;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Request;

/**
 * 剧组
 */
public class DteamListActivity extends BaseAppCompatActivity
{
    private DteamAdapter adapter;
    private Toolbar toolbar;
    private XRefreshView refreshView;
    private ListView list_view;

    private int now_page = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dteam_list);
        initToolBar();
        initListView();
        initDteamList(true,""+1);

    }



    private void initToolBar()
    {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle(getResources().getString(R.string.dteam_list));//设置主标题
        setSupportActionBar(toolbar);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true); // 决定左上角图标的右侧是否有向左的小箭头, true
        // 有小箭头，并且图标可以点击
        actionBar.setDisplayShowHomeEnabled(false);

    }


    private void initListView()
    {
        list_view = (ListView) findViewById(R.id.list_view);
        adapter = new DteamAdapter(this, new ArrayList<Dteam>());
        list_view.setAdapter(adapter);

        refreshView = (XRefreshView) findViewById(R.id.xrefreshview);
        refreshView.setPullLoadEnable(true);
        //设置刷新完成以后，headerview固定的时间
        refreshView.setPinnedTime(1000);
        refreshView.setPullLoadEnable(true);
        refreshView.setMoveForHorizontal(true);
        refreshView.setAutoLoadMore(true);
        refreshView.setEmptyView(R.layout.layout_emptyview);
        refreshView.getEmptyView().setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                refreshView.startRefresh();
            }
        });

        refreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener()
        {

            @Override
            public void onRefresh()
            {
                initDteamList(true,""+1);
            }

            @Override
            public void onLoadMore(boolean isSilence)
            {
                initDteamList(false,""+now_page);
            }
        });

    }



    private void initDteamList(final boolean isInit, String page )
    {
        ServerApi.dteam_list(page, new ServerCallBack(){

            @Override
            public void onError(Request request, Exception e) {
                if (refreshView!=null)
                {
                    refreshView.stopRefresh();
                    refreshView.stopLoadMore();
                }
            }

            @Override
            public void onResponse(String response) throws JSONException
            {
                {
                    Log.e("......",response.toString());
                     List<Dteam> dteamList = new ArrayList<Dteam>();
                    JSONObject jsonObject = new JSONObject(response);
                    if ("0".equals(jsonObject.optString("code")))
                    {
                        now_page ++;
                        if (isInit == true)
                        {
                            now_page = 2;
                        }
                        JSONObject objectData = jsonObject.optJSONObject("data");
                        JSONArray objectList = objectData.optJSONArray("list");

                        if (null != objectList && objectList.length()>0)
                        {
                            for (int i = 0; i < objectList.length(); i++) {
                                JSONObject object = objectList.optJSONObject(i);
                                Dteam dteam = Dteam.resolution(object);

                                if (dteamList != null)
                                {
                                    dteamList.add(dteam);
                                }
                            }
                        }
                        adapter.replaceOrAdd(isInit,dteamList);
                        adapter.notifyDataSetChanged();
                    }
                    else  if ("10004".equals(jsonObject.optString("code")))
                    {
                        Intent intent2 = new Intent();
                        intent2.setClass(DteamListActivity.this, LoginActivity.class);
                        startActivity(intent2);
                        DteamListActivity.this.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                    }

                    if (refreshView !=null)
                    {
                        refreshView.stopRefresh();
                        refreshView.stopLoadMore();
                    }
                }
            }
        });
    }


}
